package com.cgly.medical.service.impl;

import com.cgly.medical.domain.China;
import com.cgly.medical.domain.DrugCompany;
import com.cgly.medical.domain.MedicalPolicy;
import com.cgly.medical.mapper.ChinaMapper;
import com.cgly.medical.mapper.MedicalPolicyMapper;
import com.cgly.medical.model.CompanyPolicyModel;
import com.cgly.medical.model.MedicalPolicyModel;
import com.cgly.medical.service.MedicalPolicyService;
import com.cgly.medical.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: CGLY
 * @description:
 * @author: chen di
 * @create: 2022-08-18 09:31
 */
@Service
public class MedicalPolicyServiceImpl implements MedicalPolicyService
{

    @Resource
    MedicalPolicyMapper medicalPolicyMapper;

    @Resource
    ChinaMapper chinaMapper;


    @Override
    public Msg selectAllMedicalPolicy() {
        List<MedicalPolicy> all = medicalPolicyMapper.findAll();
        if(all.size() == 0) return Msg.fail().mess("查询失败");
        return Msg.success().data("medicalPolicies",all);
    }

    @Override
    public Msg getAllPolicyWithPage(MedicalPolicy medicalPolicy) {
        if (medicalPolicy.getSize() == 0) {
            medicalPolicy.setSize(1);
        }
        PageHelper.startPage(medicalPolicy.getPn(), medicalPolicy.getSize());//插件
        List<MedicalPolicyModel> list = medicalPolicyMapper.getAllMedicalPolicy(medicalPolicy);
        for (int i = 0; i < list.size(); i++) {
            Long cityId = list.get(i).getCityId();
            China china = chinaMapper.selectById(Math.toIntExact(cityId));
            list.get(i).setChina(china);
        }
        PageInfo<MedicalPolicyModel> info = new PageInfo<>(list, 5);//5 是下面可以选多少页

        return Msg.success().data("medicalPolicies",info);
    }

    @Override
    public Msg selectOneMedicalPolicy(Long id) {

        MedicalPolicyModel byId = medicalPolicyMapper.findById(id);
        if(byId == null) return Msg.fail().mess("根据Id查询失败");
        byId.setChina(chinaMapper.selectById(Math.toIntExact(byId.getCityId())));
        return Msg.success().data("medicalPolicy",byId);
    }

    @Override
    public Msg selectOneByCityID(Long CityId) {
        MedicalPolicy medicalPolicy = medicalPolicyMapper.findByCityId(CityId);
        if(medicalPolicy == null) return Msg.fail().mess("根据城市id查询失败");
        return Msg.success().data("`medicalPolicy`",medicalPolicy);
    }

    @Override
    public Msg insertMedicalPolicy(MedicalPolicy medicalPolicy) {
        int insert = medicalPolicyMapper.insert(medicalPolicy);
        if(insert == 0) return Msg.fail().mess("插入失败");

        return Msg.success();
    }

    @Override
    public Msg updateMedicalPolicy(MedicalPolicy medicalPolicy) {
        int update = medicalPolicyMapper.update(medicalPolicy);
        if(update == 0 ) return Msg.fail();
        return Msg.success();
    }

    @Override
    public Msg deleteMedicalPolicy(Long id) {
        int i = medicalPolicyMapper.deleteById(id);
        if(i == 0 ) return Msg.fail();
        return Msg.success();
    }
}